package com.zero365.config;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;


/**
 * <p>文件名称: Properties.java </p>
 * <p>文件描述:  它还提供了自动存盘和自动更新的功能:Automatic Reloading Automatic Saving</p>
 * <p>版权所有: 版权所有(C)2001-2004</p>
 * <p>公    司: </p>
 * <p>内容摘要: 无</p>
 * <p>其他说明: 无</p>
 * <p>创建日期：2006-11-9</p>
 * <p>完成日期：2006-11-9</p>
 * <p>修改记录1: // 修改历史记录，包括修改日期、修改者及修改内容</p>
 * <pre>
 *    修改日期：
 *    版 本 号：
 *    修 改 人：
 *    修改内容：
 * </pre>
 * <p>修改记录2：…</p>
 * @version 1.0
 * @author 杨威 */

/**
 * @author yangwei0048700358
 *
 */
public class SetupSystem {
    public static void readProperties() {
        try {
            //读取常规数据
            PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
            String backColor = config.getString("colors.background");
            //Dimension size = new Dimension(config.getInt("window.width"),config.getInt("window.height"));
            //System.out.println("height is " + size.getHeight() + " width is " +size.getWidth() + " backGround is" + backColor);

            //在这个usergui.properties 文件中也可以引用其它的文件，具体语法如下：
//            # usergui.properties
//
//            include = colors.properties
//            include = sizes.properties

            //Lists and arrays 读取数组属性

            //以数组的方式获得
//            String[] colors = config.getStringArray("colors.pie");

            //list方式
            List colorList = config.getList("colors.pie");
            Iterator iterator=colorList.iterator();
            while(iterator.hasNext()){
                System.out.println("the element is "+iterator.next());
            }

           //Variable Interpolation
            String title = config.getString("application.title");
            System.out.println("title is "+title);


        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void setProperties(){
        //出始化一个文件
        addProperties();
        File file = new File("addProperties.properties");
//      写入数据
        PropertiesConfiguration config=null;
        try
        {
            config = new PropertiesConfiguration(file.getAbsolutePath());
        }
        catch (ConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        config.setProperty("colors.background", "#FFFFFFFF");
        try
        {
            config.save();
            //另一种存盘的方式 config.save("usergui.backup.properties);
        }
        catch (ConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public static void addProperties(){
        //出始化一个文件
        File file = new File("addProperties.properties");
        if(file.exists()){
            file.delete();
        }
        try
        {
            file.createNewFile();
            System.out.println("文件出始化成功");
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //写入数据
        PropertiesConfiguration config=null;
        try
        {
            config = new PropertiesConfiguration(file.getAbsolutePath());
        }
        catch (ConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

//      Change the list delimiter character to a slash
        config.setListDelimiter('/');
//         Now add some properties
        config.addProperty("greeting", "Hello, how are you?");
        config.addProperty("colors.background", "#000000");
        config.addProperty("colors.pie",
          new String[] { "#FF0000", "#00FF00", "#0000FF" });
        config.addProperty("colors.graph", "#808080/#00FFCC/#6422FF");

//         Access data
        String salut = config.getString("greeting");
        System.out.println("salut : "+salut);
        String[] colGraph = config.getStringArray("colors.graph");
        System.out.println("colGraph : "+colGraph);
        String firstPieColor = config.getString("colors.pie");
        //另一种方式List colPie = config.getList("colors.pie");
        System.out.println("firstPieColor : "+firstPieColor);

        //存盘
        try
        {
            config.save();
        }
        catch (ConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        readProperties();
        setProperties();
        // TODO Auto-generated method stub
    }
}

